package com.uenpay.im.panel.gif;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.aswife.ui.MaskImageView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

/**
 * @author SL
 * @date 2014/7/12
 * @founcation Gif GridView
 */
public class GifGridAdapter extends BaseAdapter {
	private ViewGifCover[] gifViews;
	private LayoutInflater _inflater;

	public GifGridAdapter(Context context, ViewGifCover[] emojiArray) {
		gifViews = emojiArray;
		_inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return gifViews == null ? 0 : gifViews.length;
	}

	@Override
	public ViewGifCover getItem(int i) {
		return gifViews == null ? null : gifViews[i];
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = _inflater.inflate(R.layout.gif_item_view, viewGroup, false);
			holder.imageView = (MaskImageView) view
					.findViewById(R.id.gif_imageview);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		if (i < gifViews.length) {
			ViewGifCover mViewGif = gifViews[i];
//			Bitmap mBitmap = getImageFromAssetsFile("yutumei_cover/"
//					+ mViewGif.gifName + ".png");
//			if (mBitmap != null) {
//				holder.imageView.setImageBitmap(mBitmap);
//			}

			if(mViewGif.gifName.startsWith("http") || mViewGif.gifName.startsWith("loac_add")){
				if(mViewGif.gifName.startsWith("http")){
					holder.imageView.SetUrl(mViewGif.gifName);
				}else{
					holder.imageView.setImageResource(R.drawable.icon_add_more_gray);
				}
			}else{
				holder.imageView.setImageResource(getResourceId(mViewGif.gifName));
			}


		}
		return view;
	}

	class ViewHolder {
		MaskImageView imageView;
	}


	private int getResourceId(String resourceName){
		int drawableId = IMApplication
				.getAppContext()
				.getResources()
				.getIdentifier(
						resourceName,
						"raw",
						IMApplication.getAppContext()
								.getPackageName());
		return drawableId;
	}

//	private Bitmap getImageFromAssetsFile(String fileName) {
//		Bitmap image = null;
//		AssetManager am = IMApplication.getAppContext().getResources()
//				.getAssets();
//		InputStream is = null;
//		try {
//			is = am.open(fileName);
//			image = BitmapFactory.decodeStream(is);
//			is.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			if (is != null) {
//				try {
//					is.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				is = null;
//			}
//		}
//		return image;
//
//	}

}
